using System.Collections.Generic;
using Ioc.Kernel.Exceptions;

namespace Ioc.Kernel
{
    public class ComponentChecker : IComponentChecker
    {
        private readonly IDictionary<ContainerKey, ComponentModel> components;

        /// <summary>
        /// Initializes a new instance of the <see cref="ComponentChecker"/> class.
        /// </summary>
        /// <param name="components">The components.</param>
        public ComponentChecker(IDictionary<ContainerKey, ComponentModel> components)
        {
            this.components = components;
        }

        /// <summary>
        /// Checks the component.
        /// </summary>
        /// <param name="component">The component.</param>
        public void CheckComponent(ComponentModel component)
        {
            if (components.Values.Contains(component))
                throw new DuplicatedComponentException(component.ClassName.Name, component.Name);

            // Verifying that the component to add to the container will not duplicate
            // an existing one : ids and classes can't be duplicated
            foreach (var containerComponent in components.Values)
            {
                if (containerComponent.Name == component.Name)
                    throw new DuplicateComponentIdException(component.Name);
            }
        }
    }
}